package com.wenwei.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.wenwei.adapter.AdminAdapter;
import com.wenwei.bean.CityBean;
import com.wenwei.bean.ParkListBean;
import com.wenwei.bean.UserInfoBean;
import com.wenwei.bean.UserParkBean;
import com.wenwei.net.ApiManager;
import com.wenwei.net.BaseSubscriber;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.RogerSPUtils;
import com.wenwei.utils.UserStringUtils;
import com.wenwei.view.AdminOptDialog;
import com.wenwei.view.SubscriberPw;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/8/22.
 */
public class AdminActivity extends BaseActivity implements AdminAdapter.CallBack {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.recycler)
    RecyclerView recycler;

    AdminAdapter mAdapter;
    @Bind(R.id.parkName)
    TextView parkName;
    @Bind(R.id.parkNun)
    TextView parkNun;
    @Bind(R.id.parkAddr)
    TextView parkAddr;
    @Bind(R.id.img)
    CircleImageView img;
    @Bind(R.id.name)
    TextView name;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_admin;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("车场管理员");
        UserInfoBean ub = RogerSPUtils.getObject(mContext, Constant.SP_USER_USER_RESULT, UserInfoBean.class);
        if (ub != null ){
            Glide.with(mContext).load(ub.getAvator()).error(R.drawable.icon1).into(img);
            name.setText(ub.getNickname());
        }
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new AdminAdapter();
        mAdapter.setCallBack(this);
        recycler.setAdapter(mAdapter);

    }


    @Override
    public void onItemClick(final int parkId) {
        final AdminOptDialog optDialog = new AdminOptDialog(mContext);
        optDialog.setDelDialogEvent(new AdminOptDialog.DelDialogEvent() {
            @Override
            public void onDelete() {
                optDialog.dismiss();
                operatePark(parkId);
            }

            @Override
            public void onCancel() {
                optDialog.dismiss();
            }
        });
        optDialog.show();
    }


    @Override
    protected void initData() {
        super.initData();
        getUserAdmin();
    }

    private BaseSubscriber<ParkListBean> parkSb;

    private void getParkList(int parkId) {
        parkSb = new BaseSubscriber<ParkListBean>(mContext) {
            @Override
            public void onSuccess(ParkListBean parkListBean) {
                mAdapter.setmList(parkListBean.getData());
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        };
        ApiManager.getInstance().getParkList(parkSb, 2, parkId, 1);
    }

    private BaseSubscriber<Object> parkCarSb;

    private void operatePark(int parkId) {
        parkCarSb = new BaseSubscriber<Object>(mContext) {
            @Override
            public void onSuccess(Object o) {
                showToast("操作成功");
                getParkList(mParkId);
            }
        };
        ApiManager.getInstance().operatePark(parkCarSb, parkId);
    }

    private ArrayList<UserParkBean> mList;
    private BaseSubscriber<ArrayList<UserParkBean>> userParkSb;
    private int mParkId;
    private void getUserAdmin(){
        userParkSb = new BaseSubscriber<ArrayList<UserParkBean>>(mContext) {
            @Override
            public void onSuccess(ArrayList<UserParkBean> userParkBeen) {
                mList = userParkBeen;
                if(userParkBeen!=null && userParkBeen.size()>0){
                    parkName.setText(userParkBeen.get(0).getParkName());
                    parkNun.setText("编号："+userParkBeen.get(0).getParkNum());
                    parkAddr.setText(userParkBeen.get(0).getParkAddress());
                    getParkList(userParkBeen.get(0).getId());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Logger.d(e.toString());
            }
        };
        ApiManager.getInstance().getUserAdmin(userParkSb, 3);
    }


    @OnClick({R.id.base_back, R.id.parkName})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.parkName:
                new SubscriberPw(AdminActivity.this, parkName, new SubscriberPw.CallBack() {
                    @Override
                    public void onClose(int position ,int id) {
                        mParkId = id;
                        parkName.setText(mList.get(position).getParkName());
                        parkNun.setText("编号："+mList.get(position).getParkNum());
                        parkAddr.setText(mList.get(position).getParkAddress());
                        getParkList(id);
                    }
                },mList);
                break;
        }
    }
}
